﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreClass
{
    public class Sign
    {
        #region 检测接口签名
        /*
         * token:签名公钥
         * timestamp:时间戳
         * sign:签名
         * signString:加入签名的字符串
         * 
         * 返回值，签名通过返回空字符串，签名错误返回错误信息
         */
        public static string check_sign(string token, string timestamp, string sign, string signString)
        {
            string json = "";
            DESEncryptor des = new DESEncryptor();
            string sign_now = des.md5(token + signString + timestamp, "utf-8");
            if (!sign_now.Equals(sign))
            {
                json = "{\"errorCode\":\"1002\",\"reason\":\"sign error\",\"result\":\"\"}";
                return json;
            }
            double timestamp1 = double.Parse(timestamp);
            //兼容时间为妙和毫秒
            if (timestamp.Length == 13)
            {
                timestamp1 = timestamp1 / 1000;
            }
            if (myString.GetTimeStamp(false) - timestamp1 > 120)
            {
                json = "{\"errorCode\":\"1003\",\"reason\":\"time error\",\"result\":\"\"}";
                return json;
            }
            return "";
        }
        #endregion

        #region 制作签名
        public static string make_sign(string token, string signString, long timestamp)
        {
            DESEncryptor des = new DESEncryptor();
            if (!string.IsNullOrEmpty(signString))
            {
                string source = token + timestamp.ToString() + signString;
                var sign = des.md5(source, "utf-8");
                return sign;
            }
            else
                return des.md5(token + timestamp.ToString(), "utf-8");
        }
        #endregion
    }
}

